/*
 * @Author: Creatore 
 * @Date: 2020-09-22 15:57:40
 * @LastEditors: Creator
 * @LastEditTime: 2020-09-26 11:12:29
 * @Description: file content
 */
#include "list.h"
#include <stdio.h>
#include <malloc.h>
#include <stdbool.h>
bool isListEmpty(Node* List)
{
    if(List == NULL)
        return 1;
    else return 0;
}
int lenList(Node* List)
{
    int num = 0;
    Node* temp = List;
    while(temp)
    {
        num++;
        temp = temp->next;
    }
    return num;
}
Node* createNode(int data)
{
    Node* temp = (Node*)malloc(sizeof(Node));
    temp->data = data;
    temp->pre = temp->next = NULL;
    return temp;
}
bool addNode(Node* List)
{
    Node* temp = List;
    Node* p = NULL;
    int data = 0;
    if(isListEmpty(List))
    {
        printf("ERROR:List hasn't been created！\n");
        return 0;
    }
    while(temp->next)
        temp = temp->next;
    printf("please input data:");
    scanf("%d",&data);
    p = createNode(data);
    temp->next = p;
    p->pre = temp;
    return 1;
}
Node* findNode(Node* List,int data)
{
    Node* temp = List;
    if(isListEmpty(List))
    {
        printf("ERROR:List hasn't been created！\n");
        return 0;
    }
    while(temp && temp->data != data)
        temp = temp->next;
    if(temp)
        return temp;
    else
    {
        printf("ERROR:There is no Node with that data in this List\n");
        return 0;
    }
}
bool delNode(Node* List,int data)
{
    Node* temp = findNode(List,data);
    if(temp)
    {
        if(temp->next)
        {
            temp->pre->next = temp->next;
            temp->next->pre = temp->pre;
        }
        else
        {
            temp->pre->next = NULL;
        }
        free(temp);
        return 1;
    }
    else
    {
        printf("ERROR:No node like this OR No List!");
        return 0;
    }
    
}
bool insertNode(Node* List,int data,int p)
{
    Node* temp = findNode(List,data);
    if(temp)
    {
        Node* new = createNode(data);
        if(p)
        {
            if(!temp->next)
            {
                new->next = temp->next;
                new->next->pre = new;
            }
            new->pre = temp;
            temp->next = new;
        }
        else
        {
            if(!temp->pre)
            {
                temp->pre->next = new;
                new->pre = temp->pre;
            }
            new->next = temp;
            temp->pre = new;
        }
        return 1;
    }
    else
    {
        printf("ERROR:No node like this OR No List!");
        return 0;
    }
    
}

bool cleanList(Node* List)
{
    if(isListEmpty(List))
    {
        printf("ERROR:The List is empty!\n");
        return 0;
    }
    Node* temp = List;
    while(temp->next)
    {
        free(List);
        temp = temp->next;
        List = temp;
    }
    free(List);
    return 1;
}

void diaplay(Node* List)
{
    if(isListEmpty(List))
    {
        printf("ERROR:The List is empty!\n");
        return 0;
    }
    Node* temp = List;
    while(temp)
    {
        printf("%d",temp->data);
        temp = temp->next;
    }
}